/*
 * ArCoLIVE - Live Internet Videoconference Environment
 * Information Technology Departament - Federal University of Alagoas - Brazil
 *
 * Copyright (C) 2005 Felipe B. Pontes (felipe@labpesquisas.tci.ufal.br)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package br.ufal.tci.nexos.arcolive.protocol.rtp;

import javax.media.Format;
import javax.media.protocol.DataSource;
import javax.media.rtp.RTPControl;

/**
 * CLASSNAME.java
 *
 * CLASS DESCRIPTION
 *
 * @see CLASSNAME
 *
 * @author <a href="mailto:felipe@labpesquisas.tci.ufal.br">Felipe Barros Pontes</a>.
 * @author <a href="mailto:leandro@labpesquisas.tci.ufal.br">Leandro Melo de Sales</a>.
 * @since 0.1
 * @version 0.1
 *
 * <p><b>Revisions:</b>
 *
 * <p><b>yyyymmdd USERNAME:</b>
 * <ul>
 * <li> VERSION
 * </ul>
 */

public class ArCoLIVERTPStreamHandlerUtil {

	public static void parseSessionAddresses(String session) throws IllegalArgumentException {
		String addr = null;
		int port;
		int ttl = 1;

	    int off;
	    String portStr = null, ttlStr = null;

	    if (session != null && session.length() > 0) {
			while (session.length() > 1 && session.charAt(0) == '/') {
			    session = session.substring(1);
			}
	
			// Now see if there's a addr specified.
			off = session.indexOf('/');
			if (off == -1) {
			    if (!session.equals(""))
				addr = session;
			} else {
			    addr = session.substring(0, off);
			    session = session.substring(off + 1);
			    // Now see if there's a port specified
			    off = session.indexOf('/');
			    if (off == -1) {
					if (!session.equals("")) {
					    portStr = session;
					}
			    } else {
					portStr = session.substring(0, off);
					session = session.substring(off + 1);
					// Now see if there's a ttl specified
					off = session.indexOf('/');
					if (off == -1) {
					    if (!session.equals("")) {
					    	ttlStr = session;
					    }
					} else {
					    ttlStr = session.substring(0, off);
					}
			    }
			}
	    }

	    if (addr == null) {
	    	throw new IllegalArgumentException();
	    }

	    if (portStr != null) {
			try {
			    Integer integer = Integer.valueOf(portStr);
			    if (integer != null) {
			    	port = integer.intValue();
			    }
			} catch (Throwable t) {
			    throw new IllegalArgumentException();
			}
	    } else {
	    	throw new IllegalArgumentException();
	    }

	    if (ttlStr != null) {
			try {
			    Integer integer = Integer.valueOf(ttlStr);
			    if (integer != null) {
			    	ttl = integer.intValue();
			    }
			} catch (Throwable t) {
			    throw new IllegalArgumentException();
			}
	    }
	}
	
	public static Format getFormat(DataSource ds) {
		RTPControl control = (RTPControl)ds.getControl("javax.media.rtp.RTPControl");
		if(control != null) {
			return control.getFormat();
		}
		return null;
	}
}